
package com.jf.cloud.supplier.controller.supplier;

import com.jf.cloud.api.auth.feign.AccountFeignClient;
import com.jf.cloud.api.auth.vo.AuthAccountVO;
import com.jf.cloud.api.supplier.constant.SupplierStatus;
import com.jf.cloud.api.supplier.vo.SupplierApiDetailVO;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.supplier.constant.AuditStatus;
import com.jf.cloud.supplier.service.SupplierAuditingService;
import com.jf.cloud.supplier.service.SupplierDetailService;
import com.jf.cloud.supplier.vo.SupplierAuditingVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author zz
 */
@RestController("supplierSupplierApplyController")
@RequestMapping("/s/apply_supplier/supplier_apply")
@Tag(name = "supplier-供应商申请")
public class SupplierApplyController {

    @Autowired
    private SupplierDetailService supplierDetailService;

    @Autowired
    private SupplierAuditingService supplierAuditingService;

    @Autowired
    private AccountFeignClient accountFeignClient;

    @GetMapping("/get_auditing_info")
    @Operation(summary = "获取供应商审核状态信息" , description = "获取供应商审核状态信息,返回值为空代表该供应商没有提交过审核信息")
    public ServerResponseEntity<SupplierAuditingVO> getAuditingInfo() {
        Long supplierId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        if (Objects.equals(supplierId, Constant.DEFAULT_SHOP_ID)) {
            return ServerResponseEntity.success(null);
        }
        SupplierAuditingVO supplierAuditingVO = supplierAuditingService.getBySupplierId(supplierId);
        if (Objects.isNull(supplierAuditingVO)) {
            return ServerResponseEntity.success(null);
        }
        ServerResponseEntity<AuthAccountVO> responseEntity = accountFeignClient.getAccountInfoByTenantId(supplierId, sysType);
        SupplierApiDetailVO supplierApiDetailVO = supplierDetailService.getBySupplierId(supplierId);
        if (Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OFFLINE.value())) {
            supplierAuditingVO.setStatus(AuditStatus.OFFLINE.value());
        } else if (Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OFFLINE_AWAIT_AUDIT.value()) || Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OPEN_AWAIT_AUDIT.value())) {
            supplierAuditingVO.setStatus(AuditStatus.WAITAUDIT.value());
        }
        if (responseEntity.isSuccess() && Objects.nonNull(responseEntity.getData())) {
            AuthAccountVO authAccountVO = responseEntity.getData();
            supplierApiDetailVO.setSupplierAccount(authAccountVO.getPhone());
            supplierApiDetailVO.setAccountStatus(authAccountVO.getStatus());
        }
        supplierAuditingVO.setSupplierDetail(supplierApiDetailVO);
        supplierAuditingVO.setUserId(null);
        supplierAuditingVO.setSupplierId(null);
        supplierAuditingVO.setAuditorId(null);
        return ServerResponseEntity.success(supplierAuditingVO);
    }

}
